By Dimitre Novatchev |
Language | XSLT |
Category | xml, designpatterns, msxml |
Posted | 07 May 2001 |
Updated | 07 May 2001 |
Summary |
This snippet demonstrates how to group elements and their sub-elements calculating totals and sub-totals. The example has two levels of nesting only, however the demonstrated technique can be generalised for multiple levels. |
The following question was asked on the microsoft.public.xsl newsgroup: |
How to transform weights.xml file (see the xml code), generated from recordset, into totals.xml? <code/> and <weight/> are already pre-sorted. |
Code |
|
XSL Stylesheet |
|
The following should be noted: |
1. The Muenchian technique is used to process all different elements (the first of each set of |
|
2. The total is just a summation of the "weight" siblings of all <code> elements with the same value: |
|
3. All different <nr> children of a given <code> element are specified again using the Muenchian method (only the first out of each group of <nr>-s with the same value). To ensure that only the <nr> children of the current <code> element will be specified, a different xsl:key, named kCodeNr is used. This key yields all <code><nr> combinations, where the <nr>-s must be children of the >code<: |
|
4. The sub-total for all |
|
XML Source |
XSL Stylesheet |
XML/HTML Result: |
Text Result: |